package com.bw.module_service.ui

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_storage.mmkv.AuthMMKV
import com.bw.module_service.R
import com.bw.module_service.adapter.CarAdapter
import com.bw.module_service.databinding.ActivityCarManagerBinding
import com.bw.module_service.model.CarModel
import com.bw.module_service.model.ServiceModel
import com.bw.module_service.utils.ToastUtils
import com.bw.module_service.viewmodel.ServiceViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.CarManagerActivity)
class CarManagerActivity : BaseActivity<ServiceViewModel, ActivityCarManagerBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_car_manager


    private val mViewModel: ServiceViewModel by viewModels()
    private var mAdapter: CarAdapter = CarAdapter()
    private var positionAll: Int = 0

    override fun initData() {
        super.initData()

        //搜索
        mBinding.search.setOnClickListener {
            val input = mBinding.searchEt.text.toString().trim()
            if (input.isBlank()) {
                Toast.makeText(this, "请输入搜索内容", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            // 判断是否为纯数字（即 carId）
            if (input.all { it.isDigit() }) {
                try {
                    val carId = input.toInt()
                    mViewModel.getCar(carId)
                } catch (e: NumberFormatException) {
                    Toast.makeText(this, "车辆ID格式不正确", Toast.LENGTH_SHORT).show()
                }
            } else {
                // 非纯数字，认为是车牌号或其他字符串搜索条件
                mViewModel.byNumGetCar(input)
            }

        }

        //返回
        mBinding.back.setOnClickListener {
            finish()
        }

        //添加
        mBinding.addCar.setOnClickListener {
            if(AuthMMKV.token==""){
                ToastUtils.showLong(this, "请先登录")
                ARouter.getInstance().build(Router.Ui.LoginActivity).navigation()
                return@setOnClickListener
            }
            ARouter.getInstance().build(Router.Ui.PostCarActivity).navigation()
        }


        mAdapter.addChildClickViewIds(R.id.car_delete,R.id.car_repair)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            positionAll = position
            when (view.id) {
                R.id.car_delete -> {
                    mViewModel.deleteCar(mAdapter.getItem(position).id)
                }
                R.id.car_repair -> {
                    ARouter.getInstance().build(Router.Ui.AddCarActivity).withSerializable("car", mAdapter.data[position]).navigation()
                }
            }
        }

    }


    override fun initView() {
        mBinding.carRv.layoutManager = LinearLayoutManager(this)
        mBinding.carRv.adapter = mAdapter
        mAdapter.notifyDataSetChanged()

    }

    override fun initListener() {
        mViewModel._carLiveData.observe(this) {
            mAdapter.setList(listOf(it))
            mAdapter.notifyDataSetChanged()
        }
        mViewModel._byNumCarLiveData.observe(this) {
            mAdapter.setList(listOf(it))
            mAdapter.notifyDataSetChanged()
        }

        mViewModel._deleteCarLiveData.observe(this) {
            if (it) {
                ToastUtils.showLong(this, "删除成功")
                mAdapter.removeAt(positionAll)

            } else {
                ToastUtils.showLong(this, "删除成功")
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mBinding.searchEt.text.clear()
        mAdapter.data.clear()
        mAdapter.notifyDataSetChanged()

    }
}